<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class SitemapController extends AbstractActionController
{
	protected $SejourTable;
	protected $PaysTable;
	protected $RegionsTable;
	protected $PaysRegionsTable;
	protected $ActivitesSejourTable;
	protected $AgenceTable;
	protected $StringService;

    public function indexAction() {
		$activites = $this->getActivitesSejourTable()->fetchAllUsed();
		$agences = $this->getAgenceTable()->fetchAllUsed();

		$paysregions = $this->getPaysRegionsTable()->fetchAllUsed();

    	return new ViewModel(array(
	        'paysregions' => $paysregions,
	        'activites' => $activites,
	        'agences' => $agences
	    ));

    }

    public function listesejoursAction() {
    	$config = $this->getServiceLocator()->get('Config');
    	$root_url = $config['root_url'];

    	$sejours = $this->params()->fromRoute('sejours');
    	$titreFiltre = $this->params()->fromRoute('titreFiltre');

    	$this->layout()->setVariable("actionCustomVariable", $titreFiltre);
    	
    	return new ViewModel(array(
    		'root_url' => $root_url,
	        'listeSejours' => $sejours,
	        'titreFiltre' => $titreFiltre,
	        'StringService' => $this->getStringService()
	    ));
    }

    public function paysAction() {

    	$paysUrl = $this->params()->fromQuery('v');

    	$pays = $this->getPaysTable()->getPaysByLibelle( $paysUrl );

    	$sejoursFiltres = $this->getSejourTable()->filtrerLesSejours( $pays );

		if( !is_array($sejoursFiltres) ) {
			$sejoursFiltres = array( $sejoursFiltres );
		}

    	return $this->forward()->dispatch('Application\Controller\Sitemap', array(
    	  'action'	=> 'listesejours',
	      'sejours' => $sejoursFiltres,
	      'titreFiltre' => "Séjours en ".$pays->libellePays
	    ));
    }

    public function regionAction() {
    	$regionUrl = $this->params()->fromQuery('v');

    	$regions = $this->getRegionsTable()->getRegionByLibelle( $regionUrl );

    	$sejoursFiltres = $this->getSejourTable()->filtrerLesSejours( $regions );

    	if( !is_array($sejoursFiltres) ) {
			$sejoursFiltres = array( $sejoursFiltres );
		}

    	return $this->forward()->dispatch('Application\Controller\Sitemap', array(
    	  'action'	=> 'listesejours',
	      'sejours' => $sejoursFiltres,
	      'titreFiltre' => "Séjours en ".$regions->libelleRegions
	    ));
    }

    public function activiteAction() {
    	$datasUrl = $this->params()->fromQuery('v');

    	$activites = $this->getActivitesSejourTable()->getActiviteByLibelle( $datasUrl );

    	$sejoursFiltres = $this->getSejourTable()->filtrerLesSejours( $activites );

    	if( !is_array($sejoursFiltres) ) {
			$sejoursFiltres = array( $sejoursFiltres );
		}

    	return $this->forward()->dispatch('Application\Controller\Sitemap', array(
    	  'action'	=> 'listesejours',
	      'sejours' => $sejoursFiltres,
	      'titreFiltre' => "Séjours avec comme activité ".$activites->libelleAS
	    ));
    }

    public function agenceAction() {
    	$datasUrl = $this->params()->fromQuery('v');

    	$agences = $this->getAgenceTable()->getAgenceByLibelle( $datasUrl );

    	$sejoursFiltres = $this->getSejourTable()->filtrerLesSejours( $agences );

    	if( !is_array($sejoursFiltres) ) {
			$sejoursFiltres = array( $sejoursFiltres );
		}

    	return $this->forward()->dispatch('Application\Controller\Sitemap', array(
    	  'action'	=> 'listesejours',
	      'sejours' => $sejoursFiltres,
	      'titreFiltre' => "Séjours proposés par l'agence ".$agences->libelle
	    ));
    }

	/*************************************************************************************************************/


	public function getSejourTable()
	{
		if( !$this->SejourTable ){
			$sm = $this->getServiceLocator();
			$this->SejourTable = $sm->get("Application\Model\SejourTable");
		}
		return $this->SejourTable;
	}

	public function getPaysTable()
	{
		if( !$this->PaysTable ){
			$sm = $this->getServiceLocator();
			$this->PaysTable = $sm->get("Application\Model\PaysTable");
		}
		return $this->PaysTable;
	}
	
	public function getRegionsTable()
	{
		if( !$this->RegionsTable ){
			$sm = $this->getServiceLocator();
			$this->RegionsTable = $sm->get("Application\Model\RegionsTable");
		}
		return $this->RegionsTable;
	}

	public function getActivitesSejourTable()
	{
		if( !$this->ActivitesSejourTable ){
			$sm = $this->getServiceLocator();
			$this->ActivitesSejourTable = $sm->get("Application\Model\ActivitesSejourTable");
		}
		return $this->ActivitesSejourTable;
	}

	public function getAgenceTable()
	{
		if( !$this->AgenceTable ){
			$sm = $this->getServiceLocator();
			$this->AgenceTable = $sm->get("Application\Model\AgenceTable");
		}
		return $this->AgenceTable;
	}

	public function getPaysRegionsTable()
	{
		if( !$this->PaysRegionsTable ){
			$sm = $this->getServiceLocator();
			$this->PaysRegionsTable = $sm->get("Application\Model\PaysRegionsTable");
		}
		return $this->PaysRegionsTable;
	}

	public function getStringService()
	{
		if( !$this->StringService ){
			$sm = $this->getServiceLocator();
			$this->StringService = $sm->get("StringService");
		}

		return $this->StringService;
	}
}
